<template>
  <div @keyup.enter="getAllData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="项目名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          :auto-top="true"
          @change="getAllData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getAllData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目立项')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.city_res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.city_res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.city_res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.city_res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.city_res_a_name }}</div>
              <div>B角:{{ scope.row.city_res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
      <template v-slot:com="scope">
        <div>
          <small class="text-muted">{{ scope.row.service_name || '-' }}</small>
        </div>
        <div>
          <small class="text-muted">{{ scope.row.bank_name || '-' }}</small>
        </div>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getAllData"
      ></el-pagination>
    </div>

    <view-dialog :visible.sync="viewVisible" :related-data="viewRow"></view-dialog>

    <el-dialog :visible.sync="letterVisible" width="840px">
      <letter-view :query-id="letterQueryId"></letter-view>
    </el-dialog>

    <attach-dialog :visible.sync="attachVisible" :related-id="attachId"></attach-dialog>

    <handle-dialog
      :visible.sync="signVisible"
      :query-id="signId"
      @success="reGetData"
    ></handle-dialog>
  </div>
</template>

<script>
import { enumGuatype, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/bigcredit/views/accept/mixins'

import SelectAddress from '@/components/select/SelectAddress'
import ViewDialog from '@/bigcredit/views/accept/approval/ViewDialog'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'

import AttachDialog from '@/bigcredit/views/accept/letter/components/AttachDialog'
import HandleDialog from '@/bigcredit/views/accept/letter/components/HandleDialog'
export default {
  components: {
    AttachDialog,
    SelectAddress,
    ViewDialog,
    LetterView,
    HandleDialog,
  },
  mixins: [baseMixins],
  data() {
    return {
      order: {
        N1is_history: 'asc',
        N2item_status: 'asc',
        N3item_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'is_approval_gua.gt': '0',
        subKey: 'entrust_contract.customer_related_id',
      },

      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '放款金额(万元)', prop: 'approvedFunds', align: 'center' } },
        { attrs: { label: '立项时间', prop: 'prelimTime', width: 100 } },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },

        // { attrs: { label: '担保机构', prop: 'service_name', align: 'center', minWidth: '110px' } },
        {
          attrs: { label: '合作机构', prop: 'comOrg', align: 'center', width: 140 },
          slot: 'com',
        },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },

        { attrs: { label: '共同责任函', prop: 'handleStatusLabel', align: 'center', width: 100 } },
        {
          attrs: { label: '操作', align: 'center', width: 130 },
          operations: [
            {
              content: '联函处理',
              icon: 'fa fa-gavel',
              event: this.handleSign,
            },
            {
              content: '上传会议纪要',
              icon: 'fa fa-clipboard',
              event: this.hanleAttach,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      viewVisible: false,
      viewRow: {},

      enumStatus: [
        { label: '未立项', value: 0 },
        { label: '已立项', value: 1 },
        { label: '已拒绝', value: 2 },
      ],
      enumParams: {
        0: {
          'item_status.eq': 0,
        },
        1: {
          'item_status.eq': 1,
        },
        2: {
          'item_status.eq': 2,
        },
      },
      selectStatus: '',
      approveVisible: false,
      relatedId: '',
      editData: {},
      letterVisible: false,
      letterQueryId: '',
      attachVisible: false,
      attachId: '',
      signVisible: false,
      signId: '',
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getAllData()
  },
  methods: {
    handleSign(row) {
      this.signVisible = true
      this.signId = row.customer_related_id
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachId = row.customer_related_id
    },
    showLetter(row) {
      if (row.docId) {
        this.letterVisible = true
        this.letterQueryId = row.customer_related_id
      }
    },
    getUnionLetter(data) {
      const ids = data.map(item => item.customer_related_id)
      return this.$api.getData(
        this.$serviceName,
        'ele_letter_info',
        {
          'customer_related_id.in': ids,
          'ele_letter_type.eq': 2,
        },
        { create_time: 'desc' }
      )
    },
    getAllData(index = 1) {
      this.loading = true
      this.getData(index)
        .then(() => {
          return this.getUnionLetter(this.tableData)
        })
        .then(letter => {
          this.tableData = this.tableData.map(it => {
            const data =
              letter?.data?.find(item => item.customer_related_id === it.customer_related_id) ?? {}
            if (data.sign_status > 0) {
              it.handleStatusLabel = '已签章'
            }
            return it
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
    async reGetData() {
      this.getAllData()
      this.$store.dispatch('getTaskCountBusSelfFast')
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type]

      item.prelimTime = this.$utils.dateFormat(item.item_date, this.dateFmt)
      item.resName = `A角: ${item.city_res_a_name} B角: ${item.city_res_b_name}`

      item.processLabel = enumProcessStatus[item.process_status] || ''

      item.comOrg = `${item.service_name}-${item.bank_name}`
      const itemEntrust = item.entrust_contract || {}
      item.handleStatusLabel = itemEntrust.entrust_contract_id ? '待签章' : '待合作机构处理委托合同'
      item.approvedFunds = itemEntrust.reply_funds || '-'
      return item
    },
    handleView(row) {
      this.viewRow = row
      this.viewVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
